package m.jclecai.com.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.ButterKnife;
import m.jclecai.com.R;
import m.jclecai.com.adapter.ExpandGroupBuyInfoAdapter;
import m.jclecai.com.bean.GroupBuyBean;
import m.jclecai.com.bean.GroupBuyDetailBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.OnItemClickListener;
import m.jclecai.com.utils.ChangeUtils;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.view.CustomExpandListview;


/**
 * Created by ZD on 2017/5/15.
 * 购买记录
 */

public class BuyDetailActivity extends LotteryRecordActivity implements OnItemClickListener {

    @Override
    public int getContentView() {
        return R.layout.activity_buy_detail;
    }

    @Override
    public void initView() {
        super.initView();

        datas = new ArrayList<>();
        expand_list = (CustomExpandListview) findViewById(R.id.expand_list);
        expand_list.addHeaderView(View.inflate(this, R.layout.header_buy_detail, null));
        expand_list.addFooterView(View.inflate(this, R.layout.footer_group_buy_detail, null));
        //覆盖BaseActivity的引用 因为使用了 View.inflate 所以必须等到全部加载了在绑定 能够得到引用
        ButterKnife.bind(this);
        setTitle("自购详情");
        leftOptionText("");
        //总进球
        adapter = new ExpandGroupBuyInfoAdapter(this, expand_list, datas, this);
        expand_list.setAdapter(adapter);
        //悬浮的标题
        expand_list.setHeaderView(getLayoutInflater().inflate(
                R.layout.item_group_buy_parent, expand_list, false));
        expand_list.findViewById(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groupBean != null) {
                    startActivity(new Intent(BuyDetailActivity.this, OrderDetailActivity.class)
                            .putExtra("id", groupBean.getId()));
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        String str = getIntent().getStringExtra("bean");
        if (!TextUtils.isEmpty(str))
            groupBean = FastOk.get(str, GroupBuyBean.class);
        userBean = PreferenceUtil.readUser(this);
        String strDetail = getIntent().getStringExtra("detailBean");
        if (TextUtils.isEmpty(strDetail))
            getData(Constant_Url.SELF_DETAIL);
        else {
            detailBean = FastOk.get(strDetail, GroupBuyDetailBean.class);
            execData();
            adapter.notifyDataSetChanged();
            for (int i = 0; i < adapter.getGroupCount(); i++) {// 默认全部展开
                expand_list.expandGroup(i);
            }
        }
        if (ivIcon != null)
            ivIcon.setImageResource(getIcon());
    }

    @Override
    protected void updateText(GroupBuyDetailBean bean) {
        String Str = "";
        //截止时间
//        BaseUtils.saleEnd(tvHeadEndTime, DateUtil.strToLong(bean.getEndtime()), 1000);
        if (tvHeadEndTime != null)
            tvHeadEndTime.setText(bean.getResult());
        if (tvHeadEndTimeDesc != null)
            tvHeadEndTimeDesc.setVisibility(View.GONE);
        //中奖金额；
        if (tvLotteryAward != null && tvLotteryAwardMoney !=null ){

            if (!TextUtils.isEmpty(bean.getWin_money()) && Float.parseFloat(bean.getWin_money())>0) {
                String awardStr=bean.getWin_money()+"元";
                String win_money = bean.getWin_money();
                DecimalFormat df = new DecimalFormat("#########.#");
                Str = df.format(Float.parseFloat(win_money)-bean.getWin_addmoney());
                if(bean.getWin_addmoney()>0){
                    awardStr+=" ( 加奖: "+bean.getWin_addmoney()+"元 )";
                }
                tvLotteryAwardMoney.setText(awardStr);

                tvLotteryAward.setVisibility(View.VISIBLE);
                tvLotteryAwardMoney.setVisibility(View.VISIBLE);
            }else{
                tvLotteryAward.setVisibility(View.GONE);
                tvLotteryAwardMoney.setVisibility(View.GONE);
            }

        }
        //开奖号码
        if(tvLottery != null && !TextUtils.isEmpty(bean.getOpencodes())){
            if (TextUtils.isEmpty(Str)){
                tvLottery.setText((bean.getOpencodes().replaceAll(",", " ")));
            }else {
                double str = Double.parseDouble(Str) ;
                tvLottery.setText(ChangeUtils.doubleTo2(str)+"元");
            }
        }
        //投注金额
        if (tvMoney != null)
            tvMoney.setText(bean.getOne_money());
        //彩种
        if (tvTitle != null)
            tvTitle.setText(bean.getType());
        //期号
        if (tvNum != null)
            tvNum.setText("第" + bean.getTermNo() + "期");
        //订单编号
        if (tvOrder != null)
            tvOrder.setText(bean.getPlanNo());
        //方案宣言
        if (tvDesc != null)
            tvDesc.setText(bean.getTeambuy_content());
        //下单时间
        if (tvTime != null)
            tvTime.setText(bean.getAddtime());
        //截止时间
        if (tvFooterEndtime != null)
            tvFooterEndtime.setText(bean.getEndtime());
        //怎么领奖
        if (tvGetGift != null)
            tvGetGift.setText("如中奖，彩金直接返还至购彩账户");
    }

    @Override
    public void onItemClick(int position) {

    }
}
